
package com.hlkj.pay.app.merchant.ext.impl;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import com.hlkj.pay.dto.merchant.ProviderChannelFeeConfigList;
import com.hlkj.pay.enums.CommonEnum;
import com.hlkj.pay.enums.PayEnums;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author HlpayTeam
 * @date 2024/11/29 10:12
 */
public class MerchantAccessCommonUtil {

    public static ProviderChannelFeeConfigList defaultFeeConfig(List<ProviderChannelFeeConfigList> wechatFeeConfigList) {
        if (CollectionUtils.isEmpty(wechatFeeConfigList)) {
            return null;
        }
        // 只获取开通成功的
        wechatFeeConfigList = wechatFeeConfigList.stream().filter(wechatFeeConfig -> CommonEnum.YES_NO_TYPE.YES.getCode().equals(wechatFeeConfig.getStatus()))
                .collect(Collectors.toList());
        if (CollectionUtils.isEmpty(wechatFeeConfigList)) {
            return null;
        }
        if (wechatFeeConfigList.size() == 1) {
            return wechatFeeConfigList.get(0);
        }
        // 优先取 扫码 然后公众号 然后小程序
        List<ProviderChannelFeeConfigList> nativeList = wechatFeeConfigList.stream()
                .filter(wechatFeeConfig -> PayEnums.SCENE_PAY_TYPE.QR.getType().equals(wechatFeeConfig.getPayWayDO().getSubType())).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(nativeList)) {
            return nativeList.get(0);
        }
        List<ProviderChannelFeeConfigList> jsapiList = wechatFeeConfigList.stream()
                .filter(wechatFeeConfig -> PayEnums.SCENE_PAY_TYPE.JSAPI.getType().equals(wechatFeeConfig.getPayWayDO().getSubType())).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(jsapiList)) {
            return jsapiList.get(0);
        }
        List<ProviderChannelFeeConfigList> miniList = wechatFeeConfigList.stream()
                .filter(wechatFeeConfig -> PayEnums.SCENE_PAY_TYPE.MINI_APP.getType().equals(wechatFeeConfig.getPayWayDO().getSubType())).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(miniList)) {
            return miniList.get(0);
        }
        return wechatFeeConfigList.get(0);
    }
}
